{extend name='public/base' /}
{block name="content"}
<div class="layui-col-md12">
    <div class="layui-card">
        <div class="layui-field-box">
            <!--搜索选项区-->
             
            <!--表格区-->
            <table class="layui-hide" id="table_id" lay-filter="table_id"></table>
        </div>
    </div>
</div>
{/block}

{block name="js"}<!--js处理区-->

<!--模板-->
<script type="text/html" id="toolbarDemo">
    <button class="layui-btn layui-btn-sm {:node('Article/add')}" lay-event="add"><i class="layui-icon"></i>新增轮播图</button>
    <button class="layui-btn layui-btn-sm {:node('Article/del')} layui-btn-danger" lay-event="delall"><i class="layui-icon layui-icon-delete"></i>批量删除</button>
    <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="reload"><i class="fa fa-refresh"></i>刷新</button>
</script>
<script type="text/html" id="operationTpl">
    <a href="javascript:;" class="layui-btn layui-btn-normal layui-btn-xs {:node('Article/edit')}" data-title="编辑"
       lay-event="edit"><i class="layui-icon"></i></a>
    <a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-xs {:node('Article/del')}" data-title="删除"
       lay-event="del"><i class="layui-icon "></i></a> 
    </a>
</script>
<script type="text/html" id="is_top">
    {{# if(d.is_top==1){ }}
    <span class="layui-badge layui-bg-blue">是</span>
    {{# }else{ }}
    <span class="layui-badge layui-bg-gray">否</span>
    {{# } }}
</script>
<script type="text/html" id="is_hot">
    {{# if(d.is_hot==1){ }}
    <span class="layui-badge layui-bg-blue">是</span>
    {{# }else{ }}
    <span class="layui-badge layui-bg-gray">否</span>
    {{# } }}
</script>
<script type="text/html" id="is_recommend">
    {{# if(d.is_recommend==1){ }}
    <span class="layui-badge layui-bg-blue">推荐</span>
    {{# }else{ }}
    <span class="layui-badge layui-bg-gray">不推荐</span>
    {{# } }}
</script>

<script>
    layui.use(['element', 'table', 'form', 'jquery', 'lucky', 'laydate'], function () {
        var element = layui.element;
        var form = layui.form;
        var table = layui.table;
        var $ = layui.jquery;
        var lucky = layui.lucky;
        var laydate = layui.laydate;
        form.render();

        //日期范围
        laydate.render({
            elem: '#time'
            , range: '~'
        });

        var tableInx=table.render({
            elem: '#table_id',
            id: 'table_id',
            url: "{:url('index')}",
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: [],
            even: false, //开启隔行背景
            page: {
                //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip', 'last'] //自定义分页布局
                , groups: 5 //只显示 1 个连续页码
                , first: false //不显示首页
                , last: false //不显示尾页
                , limit: 20
                , limits: [20, 30, 50]
            },
            //当数据太少 隐藏分页码样式：不可以，此处使用了固定高度的表格区域
            /*done: function (res, curr, count) {
                if (count <= this.limit) {
                    $('.layui-table-page').addClass('layui-hide');//总条数小于每页限制条数时隐藏分页
                }
            },*/
            height: 'full-100',
            text: {
                none: '暂无相关数据'
            },
            cols: [[
                {type:'checkbox'},
                {field: 'id', title: 'ID', width: 60},
                {field: 'title', minWidth: 150, title: '标题'},
                {
                    field: 'cates', width: 100, title: '分类', templet: function (d) {
                        tr = JSON.stringify(d.cates);
                        if ($.common.isNotEmpty(tr)) {
                            ck = JSON.parse(tr);
                            return ck.name;
                        } else {
                            return '-';
                        }
                    }
                },
                {
                    field: 'img_url', title: '封面图', width: 100, align: 'center', templet: function (d) {
                        var s = "+";
                        if ($.common.isNotEmpty(d.img_url)) {
                            s = "<img src='" + d.img_url + "' class='layui-nav-img' />";
                        }
                        return s;
                    }
                },
                {field: 'author', width: 100, title: '作者'},
                {field: 'is_top', templet: '#is_top', width: 60, title: '置顶'},
                {field: 'is_hot', templet: '#is_hot', width: 60, title: '热点'},
                {field: 'is_recommend', templet: '#is_recommend', width: 80, title: '推荐'},
                {field: 'hits',  width: 80, title: '点击量'},
                {field: 'love',  width: 80, title: '收藏量'},
                {field: 'create_time', title: '添加时间',  width: 180, sort: true},
                {templet: '#operationTpl', width: 190,  title: '操作'}
            ]],

        });

        /**
         * 关键词-状态-事件 检索表单监听事件
         */
        form.on('submit(search)', function (obj) {
            lucky.CreateSearch(obj.field);
            return false;
        });

        //要执行的操作事件
        var active = {
            add: function (obj){
                lucky.CreateOpenForm("添加文章", "{:url('add')}", '98%', '90%');
                return false;
            },
            edit: function (obj) {
                var data = obj.data;
                var _id = parseInt(data.id);
                var url = "{:url('edit')}?id=" + _id;
                lucky.CreateOpenForm("编辑文章", url,'98%', '90%');
            },
            del: function (obj){
                var data = obj.data;
                var _id = parseInt(data.id);
                lucky.FormatData(_id, "{:url('del')}", "确认删除吗？");
            },
            //批量删除
            delall:function(obj){
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                var num = 0;
                var id = [];
                for (var i in data) {
                    num++;
                    id.push(data[i].id);
                }
                if (id.length < 1) {
                    layer.msg("请至少选择一个", {icon: 15, time: 1000, shade: 0.3});
                    return false;
                }
                lucky.FormatData(id, "{:url('del')}", "确认删除吗？");
            },

            kan: function (obj){
                var data = obj.data;
                var _id = parseInt(data.id);
                var url = "{:url('kan')}?id=" + _id;
                lucky.CreateOpenForm("预览文章", url,'98%', '90%');
            },
            //刷新表格
            reload: function (obj){
                lucky.CreateReload();
            },

        }

        //监听 表头工具栏: 新增 刷新 批量删
        table.on('toolbar(table_id)', function (obj) {
            var type = obj.event
            active[type].call(this, obj);
        });
        //监听表格单行操作事件: table_id是表格过滤器名称
        table.on('tool(table_id)', function (obj) {
            var type = obj.event
            active[type].call(this, obj);
        })

    });

</script>
{/block}
